import {Injectable} from "@angular/core";
import {Contact} from "../models/contact";
import {Observable} from "rxjs";

const CONTACTS: Contact[] = [
  new Contact(21, 'Sam Spade'),
  new Contact(22, 'Nick Danger'),
  new Contact(23, 'Nancy Drew')
]

const FETCH_LATENCY = 500

@Injectable()
export class ContactService {
  getContacts(): Observable<Contact[]> {
    return Observable.of(CONTACTS).delay(FETCH_LATENCY)
  }

  getContact(id: number|string): Observable<Contact> {
    return this.getContacts().map(heroes => heroes.find(hero => hero.id === +id))
  }
}
